import sys
sys.path.append('../')
import pre_api
import time
import json


def saveOrUpdatePro(projson):
    rj_api = pre_api.pre_api_get()
    t = time.strftime("%Y-%m-%d%H", time.localtime(int(time.time())))
    json_data = projson
    response = rj_api.saveOrUpdateProduct(json_data)
    result_up = response.content.decode('utf-8')
    print(response)
    print(result_up)
    with open(f'post_json\\saveOrUpdatePro{t}.json', mode='a', encoding='utf-8') as f:
        f.write(result_up)


def UpdatePro(projson):
    rj_api = pre_api.pre_api_get()
    t = time.strftime("%Y-%m-%d%H", time.localtime(int(time.time())))
    json_data = json.dumps(''.join(json.loads(projson)))
    response = rj_api.updateProductInfoByCode(json_data)
    result_up = response.content.decode('utf-8')
    print(response)
    print(result_up)
    with open(f'post_json\\UpdatePro{t}.json', mode='a', encoding='utf-8') as f:
        f.write(result_up)



if __name__ == '__main__':
    with open('../post_json/product.json', mode='r', encoding='utf-8') as f:
        saveOrUpdatePro(f.read())